package com.cl.spark.udf;

import org.apache.spark.sql.*;
import org.apache.spark.sql.api.java.UDF1;
import org.apache.spark.sql.types.*;
import org.springframework.stereotype.Component;
import scala.Tuple2;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;

// 定义 UDF 类,取小数方法
public class FractionalPartUDF implements UDF1<Double,Double> {
    @Override
    public Double call(Double value) {
        if (value == null) {
            return null;
        }
        BigDecimal newValue = new BigDecimal(String.valueOf(value));
        BigDecimal intPart = newValue.setScale(0, RoundingMode.DOWN);
        BigDecimal fractionalPart = newValue.subtract(intPart);
        return fractionalPart.doubleValue();
    }
}
